package com.btpj.module_main.ui.project

import android.graphics.Typeface
import android.text.Html
import androidx.fragment.app.Fragment
import android.view.View
import android.widget.TextView
import androidx.lifecycle.Observer
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.btpj.jetpack.base.BaseFragment
import com.btpj.module_main.R
import com.btpj.module_main.data.entity.ProjectTabItem
import com.btpj.module_main.databinding.FragmentProjectBinding
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import kotlinx.android.synthetic.main.fragment_project.*

class ProjectFragment : BaseFragment<ProjectViewModel, FragmentProjectBinding>() {

    private var mData: MutableList<ProjectTabItem> = ArrayList()
    private lateinit var mAdapter: FragmentStateAdapter
    private lateinit var mediator: TabLayoutMediator

    companion object {

        @JvmStatic
        fun newInstance() = ProjectFragment()
    }

    override fun getLayoutId() = R.layout.fragment_project

    override fun initView() {
        //xml中设置IndicatorColor 和 textcolor 都无效果 待解？
//        tabLayout.setSelectedTabIndicatorColor(resources.getColor(R.color.tab_text_selected_color))
        tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabReselected(tab: TabLayout.Tab?) {

            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {
                val customView = tab?.customView as TextView
                customView.textSize = 14f
                customView.typeface = Typeface.DEFAULT
            }

            override fun onTabSelected(tab: TabLayout.Tab?) {
                val customView = tab?.customView as TextView
                customView.textSize = 16f
                customView.typeface = Typeface.DEFAULT_BOLD
            }

        })

        mAdapter = object : FragmentStateAdapter(childFragmentManager, lifecycle) {
            override fun getItemCount(): Int {
                return mData.size
            }

            override fun createFragment(position: Int): Fragment {
                return TabItemFragment.newInstance(mData[position].id)
            }
        }
        viewPager2.apply {
            offscreenPageLimit = ViewPager2.OFFSCREEN_PAGE_LIMIT_DEFAULT
            adapter = mAdapter
        }

        mediator = TabLayoutMediator(
            tabLayout,
            viewPager2,
            true,
            TabLayoutMediator.TabConfigurationStrategy { tab, position ->
                tab.customView = createTabView(position)
            })
        mediator.attach()
    }

    private fun createTabView(position: Int): View {
        if (mData.size > 0) {
            val item = mData[position]
            val textView = TextView(requireContext())

//            val states = arrayOfNulls<IntArray>(2)
//            states[0] = intArrayOf(android.R.attr.state_selected)
//            states[1] = intArrayOf()
//
//            val colors = intArrayOf(
//                resources.getColor(R.color.tab_text_selected_color),
//                resources.getColor(R.color.tab_text_default_color)
//            )
//            val stateList = ColorStateList(states, colors)
//            textView.setTextColor(stateList)

            textView.text = Html.fromHtml(item.name)
            return textView
        }
        return TextView(requireContext())
    }

    override fun createObserve() {
        super.createObserve()
        mViewModel.tabDataLiveData.observe(this, Observer {
            mData = it
            mAdapter.notifyDataSetChanged()
        })
    }

    override fun onDestroy() {
        super.onDestroy()
        mediator.detach()
    }
}